﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class SharedStringTableWrapper
	{
		#region Custom Properties


		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the <see cref="SharedStringItemWrapper"/> at the specified zero-based index.
		/// </summary>
		/// <param name="index">The zero-based index of the shared string table wrapper to retrieve.</param>
		/// <returns>A <see cref="SharedStringItemWrapper"/> that matches the specified index. <c>null</c> if no match is found.</returns>
		internal SharedStringItemWrapper GetStringItem(
			int index)
		{
			if (SharedStringItems.Count > index)
			{
				var result = SharedStringItems[index];
				result.IncrementReferenceCount();
				return result;
			}
			else
			{
				// Indexes are only used during package decompose and recompose, so
				// an index error here indicates an error in a template package.
				throw new IndexOutOfRangeException(string.Format("A shared string item with index {0} does not exist.", index));
			}
		}

		/// <summary>
		/// Returns the <see cref="SharedStringItemWrapper"/> that contains the passed value, creating
		/// a new string table entry if no existing entry matches the passed value.
		/// </summary>
		/// <param name="value">The string value of the shared string table wrapper to retrieve.</param>
		/// <returns>A <see cref="SharedStringItemWrapper"/> that matches the specified value.</returns>
		internal SharedStringItemWrapper GetStringItem(
			string value)
		{
			SharedStringItemWrapper result = null;

			if (SharedStringItems.Contains(value))
			{
				result = SharedStringItems.Find(value);
			}
			else
			{
				result = SharedStringItems.Add(value);
			}

			result.IncrementReferenceCount();
			return result;
		}

		#endregion
		#region Overrides


		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
